package com.kaishengit.service;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.FavDao;
import com.kaishengit.entity.Book;
import com.kaishengit.entity.Fav;
import com.kaishengit.entity.User;
import com.kaishengit.util.DateUtil;

@Named
@Transactional
public class FavService {
	@Inject
	private FavDao favDao;

	public void save(Fav fav) {
		if(fav.getTime()==null){
			fav.setTime(DateUtil.getNow());
		}
		favDao.save(fav);
	}

	public Fav findByUserAndBook(User user, Book book) {
		return favDao.findByUserAndBook(user,book);
	}

	public List<Fav> findListByProperty(User user) {
		return favDao.findListByProperty("user", user);
	}

	public Fav findById(String id) {
		return favDao.findById(id);
	}

	public void del(Fav fav) {
		favDao.del(fav);
	}

	public List<Fav> findGtProperty(String name) {
		SimpleExpression r1=Restrictions.gt(name,DateUtil.getDate());//SimpleExpression implements Criterion
		return favDao.find(r1);
	}
}
